package com.xinzhitong.www.mapper;

import com.xinzhitong.www.pojo.SystemMessage;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 系统消息相关操作
 *
 * @author 徐鑫
 */
@Repository
public interface SystemMessageMapper {

    @Select("select id, title, text, url, datetime, has_read " +
            "from system_message " +
            "where username=#{username} order by datetime desc")
    List<SystemMessage> selectSystemMessagesByUsername(@Param("username") String username);

    @Select("select id, title, text, url, datetime, has_read " +
            "from system_message " +
            "where username=#{username} and ${condition} order by has_read asc, datetime desc")
    List<SystemMessage> selectSystemMessagesByUsernameWithConditions(
            @Param("username") String username,
            @Param("condition") String condition
    );

    @Insert("insert into system_message(username, title, text, url) values(#{username}, #{title}, #{text}, #{url})")
    void insertSystemMessage(
            @Param("username") String username,
            @Param("title") String title,
            @Param("text") String text,
            @Param("url") String url
    );

    @Delete("delete from system_message where username=#{username} and id in ${idRange}")
    void deleteSystemMessage(
            @Param("username") String username,
            @Param("idRange") String idRange
    );

    @Update("update system_message set ${field}=#{value} where ${condition} ")
    void updateSystemMessage(
            @Param("field") String field,
            @Param("value") Object value,
            @Param("condition") String condition
    );

    @Select("select * from system_message where username=#{username} and id=#{id}")
    SystemMessage selectTheSystemMessage(@Param("username") String username, @Param("id") Integer id);

    @Select("select * " +
            "from system_message " +
            "where username=#{username} ORDER BY has_read asc, datetime desc limit 0, #{size}")
    SystemMessage[] selectTheNewestMessage(@Param("size") Integer size, @Param("username") String username);

    @Select("select count(0) from system_message where username=#{username} and has_read=0")
    Integer countNewSystemMessages(@Param("username") String username);

}
